package util.src.com;

import tsdownload.src.com.TsDownload;

import java.util.Queue;

/**
 * @Author: Gosin
 * @Date: 2020/11/6 17:59
 */
public class MyCallThread implements Runnable {
    public Queue<String> queue;
    public String path;
    public ReturnDoneNum obj;
    private boolean flag = true;

    public MyCallThread(Queue<String> queue, String path, ReturnDoneNum obj) {
        this.queue = queue;
        this.path = path;
        this.obj = obj;
    }

    @Override
    public void run() {
        while (!queue.isEmpty()) {
            String url = queue.poll();
            String name = url.split("/")[url.split("/").length - 1];
            TsDownload a = new TsDownload(url, path, name);
            try {
                a.download();
            } catch (Exception e) {
                flag = false;
                System.out.println("错误，添加url");
                queue.offer(url);
            } finally {
                if (flag) {
                    synchronized (this) {
                        obj.doneNumPlusPlus();
                    }
                }
            }
        }
    }
}
